<template>
  <div>
    <!-- 成本费用表查询 -->
    <TopFilter ref="form" :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhCostReportQuery"
        :isToperInfo="false"
        :isColumnFilter="false"
        :isSelectColumn="false"
        :columns="columns"
        :dataSource="checkList"
        :onColumnsChange="columns => (this.columns = columns)"
      >
      </FilterTable>
    </Spin>

    <!-- 经销商查询 -->
    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <OwnerSearch :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
        <!-- 详情展示 -->
    <Drawer :visible.sync="actionsOwner.visible" :title="actionsOwner.title" width="80%" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DetailView :formData="this.tableList" :viewStatus="this.passStatus" :requestId="this.reportId" :year="this.year" :month="this.month" @close="closeSetting" @refrish="refrishList"/>
    </Drawer>
  </div>
</template>
<script>
import moment from 'moment';
import { getOwnerCode } from '@/assets/js/auth';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { queryList, exportExcel, submitData } from '@/api/omFinance/CRReportManage/CostReportQuery';
import OwnerSearch from '../OwnerSearch';
import DetailView from './detailView';

export default {
  name: 'CostReportQuery',
  mixins: [dictionary],

  components: {
    OwnerSearch,
    DetailView
  },
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      formList: this.createdFrom1(),
      isDealer: false,
      isDisabled: true,
      exportLoading: false,
      formData: {},
      tableList: [],
      reportId: '',
      year: moment().format('YYYY'),
      month: moment().format('MM'),
      dealerCode: '',
      actionsOwner: { visible: false, title: '', type: '' },
      checkList:[],
      statusCode:'',
      passStatus:'',
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
        //展示详情
    openHandler(data){

     this.reportId = data.id; // 导出传id
     this.passStatus = data.costStatus;
        this.tableList = [
            {
              column1: '编制单位:',
              column2: data.dealerCode,
              column3: data.dealerName,
              column4: '单位：元'
            },
            {
              column1: '项    目',
              column2: '行次',
              column3: '本月数',
              column4: '本年累计数'
            },
            {
              column1: '四、期间费用',
              column2: '',
              column3: '',
              column4: ''
            },
            {
              column1: '（一）销售费用',
              column2: '10=11+12+13+14+15+16+17+18+19+27+28+29+30+31+32',
              column3: data.byxsfy,
              column4: data.bnxsfy
            },
            {
              column1: '1.包装费',
              column2: '11',
              column3: data.bybzf,
              column4: data.bnbzf
            },
            {
              column1: '2.运输费',
              column2: '12',
              column3: data.byysf,
              column4: data.bnysf
            },
            {
              column1: '3.装卸费',
              column2: '13',
              column3: data.byzxf,
              column4: data.bnzxf
            },
            {
              column1: '4.仓储保管费',
              column2: '14',
              column3: data.byccbgf,
              column4: data.bnccbgf
            },
            {
              column1: '5.保险费',
              column2: '15',
              column3: data.byxsfybxf,
              column4: data.bnxsfybxf
            },
            {
              column1: '6.展览费',
              column2: '16',
              column3: data.byzlf,
              column4: data.bnzlf
            },
            {
              column1: '7.广告费',
              column2: '17',
              column3: data.byggf,
              column4: data.bnggf
            },
            {
              column1: '8.销售服务费',
              column2: '18',
              column3: data.byxsfwf,
              column4: data.bnxsfwf
            },
            {
              column1: '9.职工薪酬',
              column2: '19',
              column3: data.byxsfyzgxc,
              column4: data.bnxsfyzgxc
            },
            {
              column1: '10.业务经费',
              column2: '27',
              column3: data.byywjf,
              column4: data.bnywjf
            },
            {
              column1: '11.委托代销手续费',
              column2: '28',
              column3: data.bywtdxsxf,
              column4: data.bnwtdxsxf
            },
            {
              column1: '12.折旧费',
              column2: '29',
              column3: data.byzjf,
              column4: data.bnzjf
            },
            {
              column1: '13.修理费',
              column2: '30',
              column3: data.byxsfyglf,
              column4: data.bnxsfyglf
            },
            {
              column1: '14.样品及产品损耗',
              column2: '31',
              column3: data.byypjcpsh,
              column4: data.bnypjcpsh
            },
            {
              column1: '15.其他',
              column2: '32',
              column3: data.byxsfyqt,
              column4: data.bnxsfyqt
            },
            {
              column1: '（二）管理费用',
              column2: '33=34+42+43+44+45+46+47+48+49+50+51+52+53+54+55+56+57+58',
              column3: data.byglfy,
              column4: data.bnglfy
            },
            {
              column1: '1.职工薪酬',
              column2: '34',
              column3: data.byglfyzgxc,
              column4: data.bnglfyzgxc
            },
            {
              column1: '2.保险费',
              column2: '42',
              column3: data.byglfybxf,
              column4: data.bnglfybxf
            },
            {
              column1: '3.折旧费',
              column2: '43',
              column3: data.byzjf,
              column4: data.bnzjf
            },
            {
              column1: '4.修理费',
              column2: '44',
              column3: data.byglfyxlf,
              column4: data.bnglfyxlf
            },
            {
              column1: '5.无形资产摊销',
              column2: '45',
              column3: data.bywxzctx,
              column4: data.bnwxzctx
            },
            {
              column1: '6.存货盘亏',
              column2: '46',
              column3: data.bychpk,
              column4: data.bnchpk
            },
            {
              column1: '7.业务招待费',
              column2: '47',
              column3: data.byywzdf,
              column4: data.bnywzdf
            },
            {
              column1: '8.差旅费',
              column2: '48',
              column3: data.byclf,
              column4: data.bnclf
            },
            {
              column1: '9.办公费',
              column2: '49',
              column3: data.bybgf,
              column4: data.bnbgf
            },
            {
              column1: '10.会议费',
              column2: '50',
              column3: data.byhyf,
              column4: data.bnhyf
            },
            {
              column1: '11.诉讼费',
              column2: '51',
              column3: data.byssf,
              column4: data.bnssf
            },
            {
              column1: '12.聘请中介机构费',
              column2: '52',
              column3: data.bypqzjjgf,
              column4: data.bnpqzjjgf
            },
            {
              column1: '13.咨询费',
              column2: '53',
              column3: data.byzxuf,
              column4: data.bnzxuf
            },
            {
              column1: '14.技术转让费',
              column2: '54',
              column3: data.byjszrf,
              column4: data.bnjszrf
            },
            {
              column1: '15.董事会费',
              column2: '55',
              column3: data.bydshf,
              column4: data.bndshf
            },
            {
              column1: '16.排污费',
              column2: '56',
              column3: data.bypwf,
              column4: data.bnpwf
            },
            {
              column1: '17.党建工作经费',
              column2: '57',
              column3: data.bydjgzjf,
              column4: data.bndjgzjf
            },
            {
              column1: '18.其他',
              column2: '58',
              column3: data.byglfyqt,
              column4: data.bnglfyqt
            },
            {
              column1: '（三）财务费用',
              column2: '59=60+61+62',
              column3: data.bycwfy,
              column4: data.bncwfy
            },
            {
              column1: '其中： 利息支出',
              column2: '60',
              column3: data.bylxzc,
              column4: data.bnlxzc
            },
            {
              column1: '利息收入',
              column2: '61',
              column3: data.bylxsr,
              column4: data.bnlxsr
            },
            {
              column1: '汇兑净损失（收益以“－”列示）',
              column2: '62',
              column3: data.byhdjss,
              column4: data.bnhdjss
            }
          ];
        
        this.actionsOwner = { visible: true, title: '编辑', type: 'show' };

    },
     //刷新
  refrishList(){
    this.getList(this.year, this.month, this.dealerCode,this.statusCode);
  },
    closeSetting() {
        this.actionsOwner.visible = false;
    },
    seacherFun() {
      this.isDealer = true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    changeHandle(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];
      this.dealerCode = params.ownerCode;
      this.statusCode = params.costStatus;
      this.getList(this.year, this.month, this.dealerCode,this.statusCode);
    },
    resetChange(params) {
      this.year = params.yearMonth.split('-')[0];
      this.month = params.yearMonth.split('-')[1];

      // this.getList(this.year, this.month, '');
      this.tableList = [];
    },
    // 导入
    importHandle() {
      this.isImport = true;
    },
    emitCloseHandler(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
        this.topFilterList.find(x => x.fieldName == 'dealerName').initialValue = dealerNames.toString();
      }
    },
    // 表单数据
    formChangeHandle(val) {
      this.saveData = Object.assign({}, this.saveData, val);
      console.log(val, this.saveData, '---奥利给---');
    },
    // 驳回
    async dataHandle(status) {
      this.formList[0].rules[0].required = true;
      this.$refs.formPanel.SUBMIT_FORM();
      if (!this.formList[0].initialValue) return;
      this.saveData.id = this.reportId ? this.reportId : ''; // Id
      this.saveData.profitStatus = status; // 状态

      const res = await submitData(this.saveData);
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '操作成功' });
        this.getList(this.year, this.month, this.dealerCode);
      }
    },
    // 查询列表
    async getList(Year = '', Month = '', dealerCode = '',statusCode = '') {
      this.exportLoading = true;
      const res = await queryList({ reportYear: Year, reportMonth: Month, dealerCode: dealerCode,costStatus:statusCode });
      this.reportId = '';
      this.tableList = [];
      if (res.resultCode == 200) {
        if (res.data) {
          this.isDisabled = false;
          const data = res.data; // 表格
          this.reportId = data.id; // 导出传id
          this.checkList = res.data;
        } else {
          this.isDisabled = true;
          this.tableList = [];
          // 驳回意见清空
          this.formList[0].rules[0].required = false;
          this.formList[0].initialValue = '';
        }
      }
      this.exportLoading = false;
    },
    // 导出
    async exportHandle() {
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;
          let param = { id: this.reportId };
          exportExcel(param)
            .then(response => {
              exportExcelFile(response.data, '成本费用表查询');
              this.exportLoading = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },

    createdFrom() {
      return [
        {
          type: 'DATE',
          label: '月度',
          fieldName: 'yearMonth',
          dateType: 'month',
          initialValue: moment().format('YYYY-MM'),
          placeholder: '请选择'
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          rules: [{ required: false, message: '请选择', trigger: 'blur' }],
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        // {
        //   type: 'INPUT',
        //   label: '经销商名称',
        //   fieldName: 'dealerName',
        //   placeholder: '',
        //   disabled: true
        // },
        {
          type: 'SELECT',
          label: '审核状态',
          fieldName: 'costStatus',
          placeholder: '',
          itemList: this.createDictList('6079')
        }
      ];
    },
    createdFrom1() {
      return [
        {
          type: 'TEXT_AREA',
          label: '驳回意见',
          fieldName: 'checkDesc',
          initialValue: '',
          selfCols: 3,
          disabled: this.isDisabled,
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          render: props => {
            return (
              <div style="text-align:center">
                <el-button size="mini" type="text" onClick={() => this.openHandler(props.row)}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
       {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提报日期',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '状态',
          dataIndex: 'costStatus',
          sorter: true,
          filter: true,
          filterType: 'input',
          filterItems: this.createDictList('6079'),
          render: props => {
            return <span>{this.createDictText(props.row.costStatus, '6079')}</span>;
          }
        },
        {
          title: '驳回原因',
          dataIndex: 'checkDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
